package com.example.front.mapper;

import com.example.common.dto.Pagination;
import com.example.front.dto.GatewaySearchDto;
import com.example.front.entity.Gateway;
import com.example.front.entity.Line;
import com.example.passport.entity.User;
import org.apache.ibatis.annotations.*;


import java.util.List;

@Mapper
public interface GatewayMapper {

    @Insert("INSERT INTO `gateway`" +
            " (`tenant_id`,`code`,`name`,`url`) VALUES" +
            " (#{tenantId}, #{code}, #{name},#{url})")
    void create(Gateway gateway);

    @Update("UPDATE `gateway` SET " +
            "`name` = #{gateway.name} " +
            ", `code` = #{gateway.code}" +
            ", `url` = #{gateway.url}" +
            " WHERE `id`=#{gateway.id} AND `tenant_id`=#{gateway.tenantId} AND `status`!=" + User.STATUS_DELETE)
    int update(@Param("gateway") Gateway gateway);

    @Select("<script>" +
            "SELECT * FROM `gateway`" +
            "<where>" +
            "`tenant_id`=#{condition.tenantId} " +
            "<if test='condition.id!=0'> AND `id`=#{condition.id}</if>" +
            "<if test='condition.name!=null'> AND `name` LIKE CONCAT('%',#{condition.name},'%') </if>" +
            "<if test='condition.code!=null'> AND `code` LIKE CONCAT('%',#{condition.code},'%') </if>" +
            "</where> AND `status`!=" + User.STATUS_DELETE +
            " ORDER BY `created_at` DESC LIMIT #{pagination.offset}, #{pagination.limit}" +
            "</script>")
    List<Gateway> findAll(@Param("condition") GatewaySearchDto condition, @Param("pagination") Pagination pagination);

    @Select("<script>" +
            "SELECT COUNT(*) FROM `gateway`" +
            "<where>" +
            "<if test='condition.id!=0'> AND `id`=#{condition.id}</if>" +
            "<if test='condition.name!=null'> AND `name` LIKE CONCAT('%',#{condition.name},'%') </if>" +
            "<if test='condition.code!=null'> AND `code` LIKE CONCAT('%',#{condition.code},'%') </if>" +
            " AND `tenant_id`=#{condition.tenantId} " +
            "</where> AND `status`!=" + User.STATUS_DELETE +
            "</script>")
    long count(@Param("condition") GatewaySearchDto condition);

    @Update("UPDATE `gateway` SET `status` ="+ User.STATUS_DELETE+
            " WHERE `id`=#{id} AND `tenant_id`=#{tenantId} AND `status`!=" + User.STATUS_DELETE)
    int deleteById(long id, long tenantId);

    @Select("SELECT * FROM gateway WHERE `tenant_id` = #{tenantId} AND `name` = #{name} AND `status`!=" + User.STATUS_DELETE+" LIMIT 1")
    Gateway findByName(@Param("tenantId") long tenantId, @Param("name") String name);

    @Select("SELECT * FROM gateway WHERE  `code` = #{code} AND `status`!=" + User.STATUS_DELETE+" LIMIT 1")
    Gateway findByCode(@Param("code") String code);

    @Select("SELECT * FROM gateway WHERE `tenant_id` = #{tenantId} AND `id` = #{id}  AND `name` = #{name} AND `status`!=" + User.STATUS_DELETE+" LIMIT 1")
    Gateway findByIdName(@Param("tenantId") long tenantId, @Param("id") long id , @Param("name") String name);

    @Select("SELECT * FROM gateway WHERE `tenant_id` = #{tenantId} AND `id` = #{id}  AND `code` = #{code} AND `status`!=" + User.STATUS_DELETE+" LIMIT 1")
    Gateway findByIdCode(@Param("tenantId") long tenantId, @Param("id") long id ,  @Param("code") String code);


    @Select("<script>" +
            "SELECT * FROM `gateway` WHERE `id` IN " +
            "<foreach item='item' index='index' collection='gatewayIds' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach>  AND `status`!=" + User.STATUS_DELETE +
            "</script>")
    List<Gateway> findGatewayByIds(@Param("gatewayIds") List<Long> gatewayIds);
}
